package chy.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import chy.entity.Leave;


public class LeaveAction extends BaseAction<Leave> {
	private Leave leave;
	private Date startDate;
	private Date endDate;
	// private LeaveService leaveService;
	private Map<String, LeaveService> leaveServicesMapping;
	private static Map<String, String> leaveTypeMap;
	static {
		leaveTypeMap = new LinkedHashMap<>();
		leaveTypeMap.put(Constants.LEAVE_ANNUAL, Constants.LEAVE_ANNUAL);
		leaveTypeMap.put(Constants.LEAVE_CASUAL, Constants.LEAVE_CASUAL);
		leaveTypeMap.put(Constants.LEAVE_MARRIAGE, Constants.LEAVE_MARRIAGE);
		leaveTypeMap.put(Constants.LEAVE_MATERNITY, Constants.LEAVE_MATERNITY);
		leaveTypeMap.put(Constants.LEAVE_SICK, Constants.LEAVE_SICK);
	}
	private static Map<String, String> statusMap;
	static {
		statusMap = new LinkedHashMap<>();
		statusMap.put(Constants.LEAVESTATUS_APPROVING, "待审批");
		statusMap.put(Constants.LEAVESTATUS_APPROVED, "已审批");
		statusMap.put(Constants.LEAVESTATUS_BACK, "已打回");
	}

	public String toEdit() {
		return "edit";
	}

	public String saveLeave() {
		leave.setCreator(getLoginEmployee());
		leave.setNextDeal((Employee) this.getSession().get(
				Constants.AUTH_EMPLOYEE_MANAGER));
		getLeaveService().saveLeave(leave);
		return "redirectList";
	}

	public String searchLeave() {
		switch (this.getLoginEmployee().getSysPosition().getNameCn()) {
		case Constants.POSITION_STAFF:
			this.stuffStatusInit();
			break;
		case Constants.POSITION_FM:
			this.fmStatusInit();
			break;
		}
		pageSupport = getLeaveService().getClaimVoucherPage(getLoginEmployee(),
				leave, startDate, endDate, pageNo, pageSize);
		return "list";
	}

	protected void stuffStatusInit() {
	}

	protected void fmStatusInit() {
		if (leave == null)
			leave = new Leave();
		if (leave.getStatus() == null)
			leave.setStatus(Constants.LEAVESTATUS_APPROVING);
	}
	
	public String getLeaveById() {
		leave = this.getLeaveService().findLeaveById(leave.getId());
		return "view";
	}
	
	public String toCheck() {
		leave = this.getLeaveService().findLeaveById(leave.getId());
		return "check";
	}
	
	public String checkLeave() {
		this.getLeaveService().checkLeave(leave);
		return "redirectList";
	}

	public Map<String, String> getLeaveTypeMap() {
		return leaveTypeMap;
	}

	public Map<String, String> getStatusMap() {
		return statusMap;
	}

	public Leave getLeave() {
		return leave;
	}

	public void setLeave(Leave leave) {
		this.leave = leave;
	}

	public LeaveService getLeaveService() {
		return leaveServicesMapping.get(this.getPositionForShort());
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public static void setLeaveTypeMap(Map<String, String> leaveTypeMap) {
		LeaveAction.leaveTypeMap = leaveTypeMap;
	}

	public Map<String, LeaveService> getLeaveServicesMapping() {
		return leaveServicesMapping;
	}

	public void setLeaveServicesMapping(
			Map<String, LeaveService> leaveServicesMapping) {
		this.leaveServicesMapping = leaveServicesMapping;
	}

	public static void setStatusMap(Map<String, String> statusMap) {
		LeaveAction.statusMap = statusMap;
	}

}
